package cn.stylefeng.guns.modular.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.entity.IdjCompany;
import cn.stylefeng.guns.modular.entity.IdjUser;
import cn.stylefeng.guns.modular.entity.IdjUserRole;
import cn.stylefeng.guns.modular.mapper.IdjCompanyMapper;
import cn.stylefeng.guns.modular.mapper.IdjUserMapper;
import cn.stylefeng.guns.modular.mapper.IdjUserRoleMapper;
import cn.stylefeng.guns.modular.service.IIdjCompanyService;
import cn.stylefeng.roses.core.util.ToolUtil;

/**
 * <p>
 * 公司认证表 服务实现类
 * </p>
 *
 * @author TANGWJ
 * @since 2020-07-07
 */
@Service
public class IdjCompanyServiceImpl extends ServiceImpl<IdjCompanyMapper, IdjCompany> implements IIdjCompanyService {
	
	@Autowired
	private IdjCompanyMapper mapper;
	
	@Autowired
	private IdjUserRoleMapper roleMapper;
	
	@Autowired
	private IdjUserMapper userMapper;

	@Override
	public LayuiPageInfo getList(Page<IdjCompany> page, String keyword, String beginTime, String endTime, String yes, String no, Integer isAudit) {
		Page<Map<String, Object>> list = mapper.getList(page, keyword, beginTime, endTime, yes, no, isAudit);
		return LayuiPageFactory.createPageInfo(list);
	}

	@Override
	public ModelAndView edit(Long companyId) {
		IdjCompany company = mapper.selectByCompanyId(companyId);
		if(company != null){
			if(ToolUtil.isNotEmpty(company.getReasonType())){
				if(company.getReasonType() == 0){
					company.setReasonTypeName("资料不全");
				}else{
					company.setReasonTypeName("虚假信息");	
				}
			}
			if(ToolUtil.isNotEmpty(company.getIsAudit())){
				if(company.getIsAudit() == 1){
					company.setReasonTypeName("已通过");
				}else{
					company.setReasonTypeName("已拒绝");;	
				}
			}
			List<String> list = new ArrayList<String>();
			if(ToolUtil.isNotEmpty(company.getAuditRejectionImg())){
				String[] split = company.getAuditRejectionImg().split(",");
				for (int i = 0; i < split.length; i++) {
					list.add(split[i]);
				}
				company.setImgList(list);
			}
		}
		ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("company", company);
        return modelAndView;
	}

	@Override
	public int setIsAudit(IdjCompany company) {
		int i = mapper.setIsAudit(company);
		if(company.getIsAudit() == 1){
			IdjUserRole role = new IdjUserRole();
			role.setUserId(company.getUserId());
			role.setUserRole(2);
			role.setCreateTime(new Date());
			roleMapper.insert(role);
			IdjUser user = new IdjUser();
			user.setCompanyId(company.getCompanyId());
			user.setUserId(company.getUserId());
			userMapper.setStatus(user);
		}
		return i;
	}
	
}

